package cn.edu.scau.cmi.mingjunhui.client;

import java.util.Iterator;
import java.util.Set;

import cn.edu.scau.cmi.mingjunhui.singleton.ChairMan;
import cn.edu.scau.cmi.mingjunhui.singleton.Marshal;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SingletonClient extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		Label labelSingleChairman = new Label("主席名字：");
		TextField textSingleChairman = new TextField();
		Button ButtonCreateChairman = new Button("创建主席");
		StringBuilder sbSingleChairman = new StringBuilder();
		Label labelSingleShow = new Label();
		
		VBox vbox = new VBox();
		vbox.setSpacing(15);
		Scene scene = new Scene(vbox, 500, 650);
		primaryStage.setScene(scene);
		
		ButtonCreateChairman.setOnAction(sigHandle ->{
			ChairMan chairman = ChairMan.getInstance(textSingleChairman.getText());
			sbSingleChairman.append("当前主席：" + chairman.getName() + " 对象：" + chairman +"\n");
			labelSingleShow.setText(sbSingleChairman.toString());
		});
		
		
		labelSingleShow.setMaxHeight(500);
		labelSingleShow.setMaxWidth(650);
		vbox.getChildren().add(labelSingleChairman);
		vbox.getChildren().add(textSingleChairman);
		vbox.getChildren().add(ButtonCreateChairman);
		vbox.getChildren().add(labelSingleShow);
		
		
		
		Label labelMuliMarshal = new Label("元帅名字：");
		TextField textMuliMarshal = new TextField();
		Button ButtonCreateMarshal = new Button("创建元帅");
		StringBuilder sbMuliMarshal = new StringBuilder();
		Label labelMuliShow = new Label();

		
		ButtonCreateMarshal.setOnAction(sigHandle ->{
			Set<Marshal> marshal = Marshal.addInstance(textMuliMarshal.getText());
			sbMuliMarshal.replace(0, sbMuliMarshal.length(), "");
			sbMuliMarshal.append("当前元帅：\n");
			Iterator<Marshal> MarshalIterator = marshal.iterator();
			Iterator<Marshal> MarshalIterator2 = marshal.iterator();
			while(MarshalIterator.hasNext()) {
				sbMuliMarshal.append(MarshalIterator.next().getName() + "  " + "对象:" + MarshalIterator2.next() + "\n");
			}
			labelMuliShow.setText(sbMuliMarshal.toString());
		});
		
		labelMuliShow.setMaxHeight(1000);
		labelMuliShow.setMaxWidth(600);
		vbox.getChildren().add(labelMuliMarshal);
		vbox.getChildren().add(textMuliMarshal);
		vbox.getChildren().add(ButtonCreateMarshal);
		vbox.getChildren().add(labelMuliShow);
		
		primaryStage.show();

	}
	

}

